package com.heima.article.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;

/**
 * @author げっしょく
 * @date 2021/11/24
 */
@Service
@Slf4j
@Transactional
public class ArticleFreemarkerServiceImpl implements ArticleFreemarkerService {
    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private Configuration configuration;

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleService apArticleService;

    /**
     * 生成静态文件上传到minIO中
     *
     * @param apArticle
     * @param content
     */
    @Async
    @Override
    public void buildArticleToMinIO(ApArticle apArticle, String content) {
        //1.获取文章内容

        if (apArticle != null && StringUtils.isNotBlank(content)) {
            try {
                //2.文章内容通过freemarker生成html文件
                Template template = configuration.getTemplate("article.ftl");
                HashMap<String, Object> map = new HashMap<>();
                map.put("content", JSONArray.parseArray(content));

                StringWriter out = new StringWriter();

                template.process(map, out);

                InputStream is = new ByteArrayInputStream(out.toString().getBytes());

                //3.把html文件上传到minio中
                String path = fileStorageService.uploadHtmlFile("", apArticle.getId() + ".html", is);
                System.out.println(path);

                //4.修改ap_article表，保存static_url字段
                LambdaUpdateWrapper<ApArticle> lad = new LambdaUpdateWrapper<>();
                lad.eq(ApArticle::getId, apArticle.getId())
                        .set(ApArticle::getStaticUrl, path);
                apArticleService.update(lad);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TemplateException e) {
                e.printStackTrace();
            }
        }
    }
}
